<template>
  <!-- 活动列表 -->
  <div >
    <FilterTable
      ref="table"
      :columns="columns"
      :dataSource="dataSource"
      :isSelectColumn="false"
      :isToperInfo="false"
      :params="flterValue"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_projram_commonForm_detail-infoGoods_01"
    >
      <template slot="controls" v-if="value.type!=='show'">
        <el-button
          v-if="this.value.rowData == null || this.value.rowData.programIssuedChannel != 25301001|| this.flag == 'push'"
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="openDialog(null,'add')"
        >添加物料</el-button>
      </template>
    </FilterTable>
    <!-- 经销商弹窗 -->
    <BaseDialog
      title="选择营销素材"
      :visible.sync="visibleDialog"
      width="800px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <addMadal v-model="initModal" @close="closeDialog" />
    </BaseDialog>
  </div>
</template>

<script>
import addMadal from './infoGoodsDialog';
import { dictionary } from '@/utils/dictMixin';

import { getInfoGoods } from '@/api/cms-api/projectOperating/program/push';

export default {
  mixins: [dictionary],
  components: {
    addMadal
  },
  props: {
    value: {
      type: Object,
      required: true
    },
    flag: {
      type: String
    }
  },
  data() {
    return {
      arr: {},
      visibleDialog: false,
      initModal: {},
      formValue: {},
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: null, //ipa 接口
      dataSource: []
    };
  },
  mounted() {
    //请求接口进行赋值，自己写吧
    // console.log('infoGoods:', this.value); //获取到行id
    //请求接口进行赋值，自己写吧
    //如果传入参数不为空，则说明是修改进入，
    if (this.value != null && (this.value.type == 'edit' || this.value.type == 'show')) {
      // console.log('infoGoods2:', this.value);
      this.init();
    }
  },
  methods: {
    //修改时，初始化页面数据

    async init() {
      this.arr.projectProgramId = this.value.rowData['projectProgramId'];
      if (this.arr.projectProgramId == '' || this.arr.projectProgramId == null) {
        this.arr.projectProgramId = -1;
      }
      this.arr.currentPage = 1;
      this.arr.pageNum = 1;
      this.arr.pageSize = 10; //dataSource不支持分页
      const res = await getInfoGoods(this.arr);
      if (res.resultCode === 200) {
        var result = res.data.records;
        this.dataSource = result; //res.data.records;
        this.formValue = this.dataSource;
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg || 'getData失败!' });
      }
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        { title: '营销素材代码', dataIndex: 'materialCode', sorter: true },
        { title: '营销素材名称', dataIndex: 'materialName', sorter: true },
        {
          title: '营销素材类型-一级',
          dataIndex: 'materialTypeOneName',
          sorter: true
        },
        {
          title: '营销素材类型-二级',
          dataIndex: 'materialTypeTwoName',
          sorter: true
        },
        {
          title: '营销素材类型-三级',
          dataIndex: 'materialTypeThreeName',
          sorter: true
        },
        {
          title: '营销素材类型-四级',
          dataIndex: 'materialTypeFourName',
          sorter: true
        },
        {
          title: '营销素材类型-五级',
          dataIndex: 'materialTypeFiveName',
          sorter: true
        }
      ];
    },
    openDialog(row, type) {
      this.visibleDialog = true;
      this.visible = true;
      const pram = {
        type: 'add',
        rowData: null
      };
      this.initModal = pram;
    },
    editHandle(index, type) {
      // debugger
      //集合的加减判断放到server端.
      this.dataSource.splice(index, 1);

      // this.formValue.splice(index, 1);
    },
    //关闭详情
    closeDialog(data) {
      //console.log('1111223:', data.data);
      // this.dataSource.push(Object.assign({}, data.data[0]));
      if (data.data != undefined) {
        if (0 == data.data.length) {
          this.$notify.warning({ title: '提示信息', message: '请选择数据!' });
          return;
        }
        data.data.forEach(element => {
          this.dataSource.push(Object.assign({}, element));
        });
        this.formValue = this.dataSource;
      }

      //返回数据逻辑自己写
      //this.visible = false;
      this.visibleDialog = data.visible; //关闭弹窗
      if (data.type === 'refresh') {
        // 重新加载
        this.flterValue = { ...this.flterValue };
      }
    }
  }
};
</script>
